/*
 *
 *  *
 *  *  *
 *  *  *  * Copyright (c) Shanghai Xing Ye, Co. Ltd.
 *  *  *  * https://bg.work
 *  *  *  *
 *  *  *  *This program is free software: you can redistribute it and/or modify
 *  *  *  *it under the terms of the GNU Affero General Public License as published by
 * t *  *  *he Free Software Foundation, either version 3 of the License.
 *
 *  *  *  *This program is distributed in the hope that it will be useful,
 *  *  *  *but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  *  *  *MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  *  *  *GNU Affero General Public License for more details.
 *
 *  *  *  *You should have received a copy of the GNU Affero General Public License
 *  *  *  *along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *   *  *
 *   *
 *
 */

package dynamic.model.web.spring.boot.model

import dynamic.model.query.mq.model.ModelMetaData
import org.springframework.beans.factory.config.BeanDefinitionHolder
import org.springframework.beans.factory.support.GenericBeanDefinition
import kotlin.reflect.full.isSuperclassOf

class ModelLineGraphNode {
    var childNode: ModelLineGraphNode?=null
    var modelMetaData: ModelMetaData?=null
    fun addChildNode(childNode: ModelLineGraphNode, parentNode: ModelLineGraphNode?=null): ModelLineGraphNode {
        if(this.isChild(childNode)){
            if(this.childNode!=null){
                this.childNode?.addChildNode(childNode,this)
            }else{
                this.childNode=childNode
            }
        }
        else{
            if(parentNode==null){
                childNode.childNode=this
                return childNode
            }
            else{
                parentNode.childNode=childNode
                childNode.childNode=this
            }
        }
        return this
    }
    private fun isChild(childNode: ModelLineGraphNode):Boolean{
        return (this.modelMetaData?.beanDefinitionHolder?.beanDefinition as GenericBeanDefinition).beanClass::class.
                isSuperclassOf((childNode.modelMetaData?.beanDefinitionHolder?.beanDefinition as GenericBeanDefinition).beanClass::class)
        //return false
    }

}